package com.bawei.controller;

import com.bawei.pojo.Attendance;
import com.bawei.pojo.UserInfo;
import com.bawei.service.AttendanceService;
import com.bawei.util.PageView;
import com.bawei.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("attendance")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;


    @Autowired
    private HttpServletRequest request;
    @Autowired
    private  HttpSession session;

    @RequestMapping("/attend/list")
    public String attendanceList(Model model, @RequestParam(defaultValue = "1") String currentPage, String signInStr, String signOutStr){
        model.addAttribute("isSignIn", "1");
        UserInfo user = (UserInfo) session.getAttribute("user");
        Attendance attendance=attendanceService.selectSignInStr(user.getId());
        if(attendance==null){
            model.addAttribute("isSignIn", "0");
        }
        PageView<Attendance> pageView=attendanceService.attendanceList(currentPage,signInStr,signOutStr,user.getId());
        model.addAttribute("signInStr", signInStr);
        model.addAttribute("signOutStr", signOutStr);
        model.addAttribute("pageView", pageView);
        return "forward:/system/attendance.jsp";
    }

    @RequestMapping("/attend/signIn")
    @ResponseBody
    public ResultEntity signIn(){
        UserInfo user = (UserInfo) session.getAttribute("user");
        ResultEntity resultEntity=attendanceService.insertSignIn(user.getId(),request);
        return resultEntity;
    }

    @RequestMapping("/attend/signOut")
    @ResponseBody
    public ResultEntity signOut(){
        String uuid = (String) session.getAttribute("uuid");
        ResultEntity resultEntity=attendanceService.updateSignOut(uuid);
        return resultEntity;
    }

}
